#include "OOPPtrTest.h"
#include <iostream>

using namespace OOPUtils;
using namespace std;

OOPPtrTest::OOPPtrTest(void)
{
}

OOPPtrTest::~OOPPtrTest(void)
{
}

void OOPPtrTest::func1 (OOPPtr< TestClass1 > pTestObj)
{
	if (pTestObj->getRefNum () != 2)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;
	OOPPtr< TestClass1Parent > pTestParent = pTestObj.cvt<TestClass1Parent> ();
	if (pTestObj->getRefNum () != 3 || pTestParent->getRefNum () != 3)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;
}

void OOPPtrTest::func2 (OOPPtr< TestClass1 > pTestArr)
{
	if (pTestArr->getRefNum () != 2)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;
	OOPPtr< TestClass1 > pTestElem = &pTestArr[1];
	if (pTestArr->getRefNum () != 3)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;
}

void OOPPtrTest::test ()
{
// 	WaitForSingleObject (hMutex, INFINITE); cout << "OOPPtrTest start" << endl; ReleaseMutex (hMutex);
	testPoint = 0;
	TestClass1 stackTestClassObj;
	OOPPtr< TestClass1 > stackTestClass = &stackTestClassObj;

	if (stackTestClass->getRefNum () != 1)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;

	func1 (stackTestClass);

	if (stackTestClass->getRefNum () != 1)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;

	OOPPtr< TestClass1 > singleTestClass = new TestClass1 ();

	if (singleTestClass->getRefNum () != 1)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;

	func1 (singleTestClass);

	if (singleTestClass->getRefNum () != 1)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;

	OOPPtr< TestClass1 > multipleTestClass = OOPPtr< TestClass1 >::newArray (2);
	if (singleTestClass->getRefNum () != 1)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;

	func2 (multipleTestClass);

	if (singleTestClass->getRefNum () != 1)
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;

	if (singleTestClass.isOnHeap () || singleTestClass.isArrOnHeap ())
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;
	if (multipleTestClass.isOnHeap () || multipleTestClass.isArrOnHeap ())
	{ WaitForSingleObject (hMutex, INFINITE); cerr << __FILE__ << "(" << __LINE__ << ": OOPPtr Test Point " << testPoint << " Fail" << endl; ReleaseMutex (hMutex);}
	++testPoint;

	OOPPtr< TestClass3 > complexClassArray = OOPPtr< TestClass3 >::newArray (5);
	complexClassArray[0].arrays = OOPPtr< TestClass2 >::newArray (4);
	OOPPtr< TestClass4 >::newArray (5);
// 	WaitForSingleObject (hMutex, INFINITE); cout << "OOPPtrTest end" << endl; ReleaseMutex (hMutex);
}